home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1992 / number1 / l8.c < prev    next >
Text File  |  1992-01-24  |  3KB  |  87 lines

  1. /* Sample linked list program. Tested with Borland C++ 3.0. */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "llist.h"
  9.  
  10. void main()
  11. {
  12.    int Done = 0, Char, TempValue;
  13.    struct LinkNode *TempPtr, *ListPtr, *TempPtr2;
  14.    char TempBuffer[MAX_TEXT_LENGTH+3];
  15.  
  16.    if ((ListPtr = InitLinkedList()) == NULL) {
  17.        printf("Out of memory\n");
  18.        exit(1);
  19.     }
  20.  
  21.    while (!Done) {
  22.       printf("\nA=add; D=delete; F=find; L=list all; Q=quit\n>");
  23.       Char = toupper(getche());
  24.       printf("\n");
  25.       switch (Char) {
  26.          case 'A':               /* add a node */
  27.             if ((TempPtr = malloc(sizeof(struct LinkNode))) == NULL)
  28.             {  printf("Out of memory\n");
  29.                exit(1);
  30.             }
  31.             printf("Node value: ");
  32.             scanf("%d", &TempPtr->Value);
  33.             if ((FindNodeBeforeValue(ListPtr,TempPtr->Value))!=NULL)
  34.             {  printf("*** value already in list; try again ***\n");
  35.                free(TempPtr);
  36.             } else {
  37.                printf("Node text: ");
  38.                TempBuffer[0] = MAX_TEXT_LENGTH;
  39.                cgets(TempBuffer);
  40.                strcpy(TempPtr->Text, &TempBuffer[2]);
  41.                InsertNodeSorted(ListPtr, TempPtr);
  42.                printf("\n");
  43.             }
  44.             break;
  45.          case 'D':               /* delete a node */
  46.             printf("Value field of node to delete: ");
  47.             scanf("%d", &TempValue);
  48.             if ((TempPtr = FindNodeBeforeValue(ListPtr, TempValue))
  49.                  != NULL) {
  50.                TempPtr2 = TempPtr->NextNode; /* -> node to delete */
  51.                DeleteNodeAfter(TempPtr);     /* delete it */
  52.                free(TempPtr2);               /* free its memory */
  53.             } else {
  54.                printf("*** no such value field in list ***\n");
  55.             }
  56.             break;
  57.          case 'F':               /* find a node */
  58.             printf("Value field of node to find: ");
  59.             scanf("%d", &TempValue);
  60.             if ((TempPtr = FindNodeBeforeValue(ListPtr, TempValue))
  61.                   != NULL)
  62.                printf("Value: %d\nText: %s\n",
  63.                  TempPtr->NextNode->Value, TempPtr->NextNode->Text);
  64.             else
  65.                printf("*** no such value field in list ***\n");
  66.             break;
  67.          case 'L':               /* list all nodes */
  68.             TempPtr = ListPtr->NextNode;  /* point to first node */
  69.             if (TempPtr == ListPtr) {     /* empty if at sentinel */
  70.                printf("*** List is empty ***\n");
  71.             } else {
  72.                do {
  73.                   printf("Value: %d\n  Text: %s\n", TempPtr->Value,
  74.                         TempPtr->Text);
  75.                   TempPtr = TempPtr->NextNode;
  76.                } while (TempPtr != ListPtr);
  77.             }
  78.             break;
  79.          case 'Q':
  80.             Done = 1;
  81.             break;
  82.          default:
  83.             break;
  84.       }
  85.    }
  86. }
  87.